package com.yict.csms.resourceplan.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yict.common.service.impl.BaseServiceImpl;
import com.yict.csms.resourceplan.dao.IMachineTimeDao;
import com.yict.csms.resourceplan.entity.MachineTime;
import com.yict.csms.resourceplan.service.IMachineTimeService;
import com.yict.csms.system.entity.DataDict;
import com.yict.csms.system.service.IDataDictService;
import com.yict.csms.system.util.DictionaryUtils;

@Service("machineTimeService")
public class MachineTimeServiceImpl extends
		BaseServiceImpl<MachineTime, Long, IMachineTimeDao> implements
		IMachineTimeService {

	private IDataDictService dataDictService;
	
	/**
	 * 查询下三更信息,即查询mydate标示的日期，和timeShift标示的更次（不含）之后的三个更次
	 * 
	 * @param mydate
	 * @param timeShift
	 * @return
	 */
	public List<MachineTime> queryNext3Time(Date mydate, String timeShift) {
		//下一更
		Date mydate1;
		String timeShift1;
		MachineTime machineTime1;
		//下第二更
		Date mydate2;
		String timeShift2;
		MachineTime machineTime2;
		//下第三更
		Date mydate3;
		String timeShift3;
		MachineTime machineTime3;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(mydate);
		List<MachineTime> list = new ArrayList<MachineTime>();
		DataDict dict = dataDictService.findById(timeShift);
		
		if(DictionaryUtils.A_TIME_BUCKET.equals(dict.getDatacode())){
			mydate1 = calendar.getTime();
			timeShift1 = dataDictService.findByCode(DictionaryUtils.B_TIME_BUCKET).getDictid();
			
			calendar.add(Calendar.DAY_OF_MONTH, 1);
			mydate2 = calendar.getTime();
			timeShift2 = dataDictService.findByCode(DictionaryUtils.C_TIME_BUCKET).getDictid();
			mydate3 = calendar.getTime();
			timeShift3 = dataDictService.findByCode(DictionaryUtils.A_TIME_BUCKET).getDictid();
		}else if(DictionaryUtils.B_TIME_BUCKET.equals(dict.getDatacode())){
			calendar.add(Calendar.DAY_OF_MONTH, 1);
			mydate1 = calendar.getTime();
			timeShift1 = dataDictService.findByCode(DictionaryUtils.C_TIME_BUCKET).getDictid();
			mydate2 = calendar.getTime();
			timeShift2 = dataDictService.findByCode(DictionaryUtils.A_TIME_BUCKET).getDictid();
			mydate3 = calendar.getTime();
			timeShift3 = dataDictService.findByCode(DictionaryUtils.B_TIME_BUCKET).getDictid();
		}else{
			mydate1 = calendar.getTime();
			timeShift1 = dataDictService.findByCode(DictionaryUtils.A_TIME_BUCKET).getDictid();
			mydate2 = calendar.getTime();
			timeShift2 = dataDictService.findByCode(DictionaryUtils.B_TIME_BUCKET).getDictid();
			calendar.add(Calendar.DAY_OF_MONTH, 1);
			mydate3 = calendar.getTime();
			timeShift3 = dataDictService.findByCode(DictionaryUtils.C_TIME_BUCKET).getDictid();
		}
		
		Map<String,Object> map1 = new HashMap<String, Object>();
		map1.put("machinedate", mydate1);
		map1.put("machinetime", timeShift1);
		List<MachineTime> list1 = this.list(map1, null, null);
		if(list1.size() > 0) 
			machineTime1 = list1.get(0);
		else {
			machineTime1 = new MachineTime();
			machineTime1.setMachinedate(mydate1);
			machineTime1.setMachinetime(timeShift1);
		}
		
		Map<String,Object> map2 = new HashMap<String, Object>();
		map2.put("machinedate", mydate2);
		map2.put("machinetime", timeShift2);
		List<MachineTime> list2 = this.list(map2, null, null);
		if(list2.size() > 0) 
			machineTime2 = list2.get(0);
		else  {
			machineTime2 = new MachineTime();
			machineTime2.setMachinedate(mydate2);
			machineTime2.setMachinetime(timeShift2);
		}
		
		Map<String,Object> map3 = new HashMap<String, Object>();
		map3.put("machinedate", mydate3);
		map3.put("machinetime", timeShift3);
		List<MachineTime> list3 = this.list(map3, null, null);
		if(list3.size() > 0) 
			machineTime3 = list3.get(0);
		else  {
			machineTime3 = new MachineTime();
			machineTime3.setMachinedate(mydate3);
			machineTime3.setMachinetime(timeShift3);
		}
		list.add(machineTime1);
		list.add(machineTime2);
		list.add(machineTime3);
		return list;
	}
	
	/**
	 * 保存业务
	 * @param vo
	 * @param voList
	 * @return
	 * @throws Exception 
	 */
	@Transactional(readOnly = false,noRollbackFor={Exception.class})
	public boolean save(MachineTime vo,List<MachineTime> voList) throws Exception{
		MachineTime pvo = this.findByShift(vo.getMachinedate(), vo.getMachinetime());
		if(pvo == null) {
			this.getBaseDao().save(vo);
		}else {
			pvo.setUpdatedate(vo.getUpdatedate());
			pvo.setUpdateperson(vo.getUpdateperson());
			pvo.setRemarks(vo.getRemarks());
		}
		
		for(MachineTime entity : voList){
			MachineTime newvo = this.findByShift(entity.getMachinedate(), entity.getMachinetime());
			if(newvo == null) {
				this.getBaseDao().save(entity);
			}else {
				newvo.setQc(entity.getQc());
				newvo.setYc(entity.getYc());
				newvo.setFl(entity.getFl());
				newvo.setRtgc(entity.getRtgc());
				newvo.setCar(entity.getCar());
			}
		}
		return true;
	}
	
	public MachineTime findByShift(Date mdate,String shiftTime){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("machinedate", mdate);
		map.put("machinetime", shiftTime);
		List<MachineTime> list = this.getBaseDao().list(map, null, 0, 0);
		MachineTime vo = null;
		if(list.size() > 0) vo = list.get(0);
		return vo;
	}

	@Resource(name = "machineTimeDao")
	public void setBaseDao(IMachineTimeDao baseDao) {
		super.setBaseDao(baseDao);
	}

	public IDataDictService getDataDictService() {
		return dataDictService;
	}

	@Resource(name = "dataDictService")
	public void setDataDictService(IDataDictService dataDictService) {
		this.dataDictService = dataDictService;
	}
}
